import Qt 4.7

Item {
    id: mainContainer
    width: 514; height: 184

    signal updatedModel
    signal callUpdateCategories

    function updateModel(m_model) {
        console.debug("QML:CategoryList - Updating Category Model");
        //daoManager.updateTotalByCategory();
        //listView.model = daoManager.getAllCategories();
        listView.model = m_model;
    }

    Button {
        x: 75; y: 0; width: 115; height: 23
        color: "transparent"; defaultColor: "transparent"; pressedColor: "gray"
        text: "+ Criar Categoria"; textColor: "#00adef"; fontPointSize: 7
        onClicked: {
            listView.model = daoManager.addCategory("");
            listView.currentIndex = 0;

            var newCoordinates = listRectangle.mapFromItem(listView.currentItem, 0, 0);
            categoryLineEdit.text = listView.model[0].name;
            categoryLineEdit.index = 0;
            categoryLineEdit.y = newCoordinates.y
            categoryLineEdit.visible = true;
        }

    }

    Rectangle {
        id: listRectangle
        x: 0 ; y: 40;
        width: 514; height: 180
        color: "transparent"


        ListView {
            id: listView
            anchors.fill: parent
            clip: true
            delegate: listCatDelegate
            highlight: Rectangle { color: Qt.lighter("gray"); opacity: 0.5; radius: 1 }
            highlightFollowsCurrentItem: true
            interactive: !categoryLineEdit.visible
        }
        Component {
            id: listCatDelegate

            Rectangle {
                id: wrapper

                x: -10
                height: 40; width: 600
                color: "transparent"
                border { width: 1; color: Qt.lighter("gray")}

                function turnOffEdit() {
                    console.debug("QML:CategoriaView - Trying to turn off the edit mode: " + index);
                    var catModel = ListView.view.model;
                    for (var xcount = 0; xcount < catModel.count; xcount++) {
                        catModel[xcount].editMode = false;
                    }
                    categoryLineEdit.visible = false;
                }
                function changeCategoryName() {
                    var newName = categoryLineEdit.text;
                    var indexCat = categoryLineEdit.index;
                    console.debug("QML:CategoriaView - Trying to change the category name: " + indexCat + " with new name " + newName);
                    ListView.view.model = daoManager.updateCategory(newName, indexCat);
                    turnOffEdit();
                }
                function enableEdit() {
                    ListView.view.model[index].editMode = true;
                }

                Column {
                    x: 85; y: 14
                    //width:
                    Text {
                        font.pointSize: 6;
                        font.weight: Font.DemiBold;
                        font.family: "Series 60 Sans"
                        text: model.modelData.name; visible: !model.modelData.editMode
                    }
                }
                Column {
                    x: 340; y: 14
                   //width:
                    Text {
                        font.pointSize: 6;
                        font.weight: Font.DemiBold;
                        font.family: "Series 60 Sans"
                        text: model.modelData.total
                    }

                }

                MouseArea {
                    enabled: !model.modelData.editMode
                    anchors.fill: wrapper
                    onPositionChanged: {
                        if (popupButton.visible && popupButton.itemNumber != index) {
                            popupButton.visible = false;
                        }
                        if (categoryLineEdit.visible)
                            changeCategoryName();
                    }
                    onClicked: {
                        if (popupButton.visible ) {
                            popupButton.visible = false;
                        }
                        if (categoryLineEdit.visible)
                            changeCategoryName();
                        console.debug(ListView.view.indexAt(0,wrapper.y));
                        // Updating the current index to ensure the correct highlight
                        ListView.view.currentIndex = ListView.view.indexAt(100, wrapper.y);
                    }
                    onPressAndHold: {
                        if (categoryLineEdit.visible)
                            changeCategoryName();
                        // Updating the current index to ensure the correct highlight
                        ListView.view.currentIndex = ListView.view.indexAt(100, wrapper.y);
                        // Find out the correct coordinates of the popup button
                        var newCoordinates = listRectangle.mapFromItem(wrapper, mouse.x, mouse.y);
                        console.debug(mouse.y);
                        console.debug(newCoordinates.y);
                        popupButton.visible = false;
                        popupButton.itemNumber = ListView.view.currentIndex;
                        if (newCoordinates.y + 50 > listRectangle.height)
                            popupButton.y = newCoordinates.y - 40;
                        else popupButton.y = newCoordinates.y + 10;
                        popupButton.visible = true;
                    }
                }
            }
        }
        PopupButton {
            id: popupButton
            visible: false
            x: 156;
            onEditClicked: {
                listView.currentItem.enableEdit();
                // Find out the correct coordinates of the popup button
                var newCoordinates = listRectangle.mapFromItem(listView.currentItem, 0, 0);
                categoryLineEdit.text = listView.model[listView.currentIndex].name;
                categoryLineEdit.index = listView.currentIndex;
                categoryLineEdit.y = newCoordinates.y
                categoryLineEdit.visible = true;
                visible = false;
                mainContainer.callUpdateCategories()
            }
            onRemoveClicked: {
                listView.model = daoManager.removeCategory(listView.currentIndex);
                visible = false;
                mainContainer.callUpdateCategories()
            }
        }
        LineEdit {
            id: categoryLineEdit
            height: 40; width: 230; x: 75
            text: "";
            visible: false;
            fontPointSize: 6;
//            onTextFilled: changeCategoryName(text)

            property int index: -1

        }

    }
}
